import datetime
import json
from typing import Dict, List


class IOUtils:

    @staticmethod
    def load_dict(file)->Dict:
        _json_str = IOUtils.load_text(file)
        return json.loads(s=_json_str)

    @staticmethod
    def save_dict(data:Dict,file):
        json_str = json.dumps(data)
        IOUtils.save_text(json_str,file)

    @staticmethod
    def save_text(text:str,file):
        with open(file, "w", encoding="utf-8") as f:
            f.write(text)

    @staticmethod
    def load_text(file)->str:
        with open(file, encoding="UTF-8") as f:
            text = f.read()
        return text

    @staticmethod
    def load_date_list(file) -> List[datetime.date]:
        try:
            _data = IOUtils.load_dict(file)
            value_list = _data['dates']
            return [ datetime.date.fromordinal(v)  for v in value_list]
        except Exception:
            return []

    @staticmethod
    def save_date_list(data: List[datetime.date], file):
        value_list = [ d.toordinal()  for d in data]
        _data = {"dates":value_list}
        IOUtils.save_dict(_data,file)



if __name__ == '__main__':
    j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}'

    wdw = {}
    list = [{"ww":1},{"sw":2}]
    wdw['w'] = list
    dates = [datetime.date(2000,3,2),datetime.date(2020,3,5)]
    IOUtils.save_dict(wdw,"ss.json")

    d = IOUtils.load_dict("ss.json")
    print(f"{d}")

